﻿using System;
using System.Web;

namespace msBsc.Cls
{
    public class BPCustom : BPage
    {

        public override void Dispose()
        {
            base.Dispose();
        }

        //public msBsc.AppLogic.Cfg.UserInfo oUserInfo = new msBsc.AppLogic.Cfg.UserInfo();

        public BPCustom()
        {
        }


        public void SetLogin(HttpContext opage, bool blRedirect)
        {
            if (opage == null)
            {
                opage = HttpContext.Current;
            }

            if (!msBsc.SvRef.UserInfo.Online)
                //if (!msBsc.SvRef.UserInfo.Online || msBsc.SvRef.UserInfo.OperType != "2")
            {
                //msBsc.SvRef.UserInfo.ExitSys();
                if (!string.IsNullOrEmpty(opage.Request.Params["SETLOGIN"]))
                {
                    string strSETLOGIN = opage.Request.Url.ToString();
                    strSETLOGIN = strSETLOGIN.Substring(strSETLOGIN.IndexOf("SETLOGIN=") + 9, strSETLOGIN.Length - strSETLOGIN.IndexOf("SETLOGIN=") - 9);
                    string strLogin = msBsc.AppLogic.Cfg.DesCry.encMe(strSETLOGIN, "zbhscsys");

                    string strActDt = this.GetParamValue(strLogin, "ACTDT");
                    if (strActDt != string.Empty && DateTime.Parse(strActDt).CompareTo(DateTime.Now.AddMinutes(15)) > 0)
                    {
                        string strUser = this.GetParamValue(strLogin, "USER");
                        string strPass = this.GetParamValue(strLogin, "PASS");
                        msBsc.SvRef.UserInfo.Login(strUser, strPass);
                    }
                }
                else //if(!IsPostBack && !IsCallback)
                {
                    if (IsPostBack || isAjaxLoad)
                    {
                        throw new Exception("会话或服务器缓存已过期，请重新登录！");
                    }
                    if (blRedirect)
                    {
                        string strRetUrl = opage.Request.RawUrl;
                        int itm1 = strRetUrl.IndexOf("?");
                        if (itm1 > 0)
                        {
                            strRetUrl = strRetUrl.Substring(0, itm1) +"?"+ opage.Request.QueryString.ToString();
                        }
                        if (msBsc.SvRef.UserInfo.Online)
                        {
                            opage.Response.Redirect("/wp_UserLogin.htm?OL=1&ReturnUrl=" + strRetUrl);
                        }
                        else
                        {
                            opage.Response.Redirect("/wp_UserLogin.htm?ReturnUrl=" + strRetUrl);
                        }
                        //opage.Response.Redirect("/wp_UserLogin.htm?ReturnUrl=" + strRetUrl);
                        //opage.Response.Redirect(msBsc.AppLogic.Cfg.SystemFileDir.FileDir.UrlBase + "s_Login/?ReturnUrl=" + strRetUrl);
                    }
                }
            }
            //0后台用户，1客户，2地接客
            if (msBsc.SvRef.UserInfo.OperType != "1")
            //if (msBsc.SvRef.UserInfo.OperType != "2")
            {
                opage.Response.Redirect("/");
                //throw new Exception("你没有权限访问！");
            }
        }

        public string GetCmpLoginString(bool blCurrLogin)
        {
            HttpServerUtility Server = HttpContext.Current.Server;
            string strUser = msBsc.SvRef.UserInfo.Operator;
            string strPass = "";// this.OperatorPass;
            if (!blCurrLogin)
            {
                strUser = msBsc.SvRef.ApplicationConst.Const.GetAppSettings("CmpSystemUserNme");
                strPass = msBsc.SvRef.ApplicationConst.Const.GetAppSettings("CmpSystemUserPass");
            }
            string strRet = "SETLOGIN=" + msBsc.AppLogic.Cfg.DesCry.uncMe("USER|" + strUser + ";PASS|" + strPass + ";ACTDT|" + DateTime.Now.ToString(), "zbhscsys");//("USER|" + strUser + ";PASS|" + strPass + ";ACTDT|" + DateTime.Now.ToString(), CUtil.EncryptionType.RiJ);
            return strRet;
        }

        public override string GetExecJScript()
        {
            //ShowAllbtTip(true);
            return base.GetExecJScript();
        }

        public override void PreDoAction() //函数名改为 DoAction 界面层传至中间层的所有的操作都由此函数来进行后台处理
        {
            //try
            //{

                SetLogin(null, false);


                if (strAction == "doShowUserJobList")
                {
                    //ShowFwsfUserJobList(GetParamHtValue("ID"));
                }
                //else if (strAction == "doShowTipBytile")
                //{
                //    doShowTipBytile(GetParamHtValue("TITLE_TXT"));
                //}
                //else if (strAction == "doShowPageJobList")
                //{
                //    ShowPageJobList(GetParamHtValue("SHOW_PAGE_JOB_L_ID"));
                //}
                //else if (strAction == "doChangeJobpstautss")
                //{
                //    ChangeJobpstautss(GetParamHtValue("SHOW_PAGE_JOB_L_ID"), GetParamHtValue("SHOW_PAGE_JOB_L_OP"), GetParamHtValue("SHOW_PAGE_JOB_L_NS"));
                //}
                //else if (strAction == "doPageJobBeClick")
                //{
                //    doPageJobBeClick(GetParamHtValue("SHOW_PAGE_JOB_L_ID"), GetParamHtValue("SHOW_PAGE_JOB_L_WID"));
                //} 
            //}
            //catch (Exception err)
            //{
            //    this.Alert("服务器端错误：" + err.Message);
            //}
            base.PreDoAction();
        }

        protected override void OnInit(EventArgs e)
        {
            if (!this.isAjaxLoad)
            {
                SetLogin(null, true);//登录验证
            }

            base.OnInit(e);

        }

        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);
        }
    }
}